/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Gravena
 */
public class TestExecutor implements Runnable {

    private String path; /* Hold the entire execution line (path + program name + arguments */
    public TestExecutor(String path, String[] args) {
        this.path = path;
        
        if(args != null) {
            for(int i=0;i<args.length;i++) {
                this.path += " " + args[i];
            }
        }
    }        
    
    @Override
    public void run() {
        Runtime rt = Runtime.getRuntime();
        try {
            System.out.println("binaries/" + this.path);
            /* Execute the binary */
            Process pr = rt.exec("binaries/" + this.path);
            /* Gets the normal output of the execution */
            BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            
            /* For now, just prints to the command line, but the idea is to store that on a file and send back to the master */
            String line=null;
 
            while((line=reader.readLine()) != null) {
                System.out.println(line);
            }
            
            /* Catch the exit status */
            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);
        } catch (IOException ex) {
            
        } catch (InterruptedException ex) {
            
        }
    }    
}
